package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.request.sys.SysMenuAddRequest;
import com.mars.common.request.sys.SysMenuUpdateRequest;
import com.mars.common.request.sys.SysProductClassAddUpdateRequest;
import com.mars.common.request.sys.SysProductClassQueryRequest;
import com.mars.common.response.PageInfo;
import com.mars.common.response.sys.SysMenuResponse;
import com.mars.common.response.sys.SysProductClassResponse;
import com.mars.framework.exception.ServiceException;
import com.mars.module.admin.entity.SysDictData;
import com.mars.module.admin.entity.SysProductClass;
import com.mars.module.admin.mapper.SysDictDataMapper;
import com.mars.module.admin.mapper.SysProductClassMapper;
import com.mars.module.admin.request.SysProductClassRequest;
import com.mars.module.admin.service.ISysProductClassService;
import com.mars.module.system.entity.SysMenu;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * 产品分类业务层处理
 *
 * @author mars
 * @date 2024-01-22
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysProductClassServiceImpl implements ISysProductClassService {

    private final SysProductClassMapper baseMapper;

    @Override
    public List<SysProductClassResponse> tree(SysProductClassQueryRequest queryDto) {
        List<SysProductClass> list = baseMapper.selectByCondition(queryDto);
        List<SysProductClassResponse> voList = new ArrayList<>();
        for (SysProductClass sysMenu : list) {
            SysProductClassResponse sysMenuVo = new SysProductClassResponse();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    public List<SysProductClassResponse> getChildren(List<SysProductClass> list, Long id) {
        List<SysProductClassResponse> voList = new ArrayList<>();
        for (SysProductClass sysMenu : list) {
            SysProductClassResponse sysMenuVo = new SysProductClassResponse();
            if (sysMenu.getParentId() != null && sysMenu.getParentId().equals(id)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysProductClassAddUpdateRequest addDto) {
        SysProductClass productClass=SysProductClass.builder().build();
        BeanUtils.copyProperties(addDto, productClass);
        productClass.setParentId(addDto.getParentId() == null ? 0L : addDto.getParentId());
        baseMapper.insert(productClass);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysProductClassAddUpdateRequest updateDto) {
        SysProductClass entity = SysProductClass.builder().build();
        BeanUtils.copyProperties(updateDto, entity);
        entity.setParentId(updateDto.getParentId() == null ? 0L : updateDto.getParentId());
        baseMapper.updateById(entity);
    }

    @Override
    public SysProductClassResponse get(Long id) {
        SysProductClass productClass = baseMapper.selectById(id);
        if (Objects.isNull(productClass)) {
            throw new ServiceException("数据不存在");
        }
        SysProductClassResponse classResponse = new SysProductClassResponse();
        BeanUtils.copyProperties(productClass, classResponse);
        return classResponse;
    }

    @Override
    public SysProductClass getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysProductClass> pageList(SysProductClassRequest request) {
        Page<SysProductClass> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysProductClass> query = this.buildWrapper(request);
        IPage<SysProductClass> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysProductClass> list(SysProductClassRequest request) {
        LambdaQueryWrapper<SysProductClass> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysProductClass> buildWrapper(SysProductClassRequest param) {
        LambdaQueryWrapper<SysProductClass> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getClassName())) {
            query.like(SysProductClass::getClassName, param.getClassName());
        }
        if (param.getParentId() != null) {
            query.eq(SysProductClass::getParentId, param.getParentId());
        }
        return query;
    }



}
